/* 
 * ***** BEGIN LICENSE BLOCK ***** Version: NPL 1.1/GPL 2.0/LGPL 2.1 The
 * contents of this file are subject to the Netscape Public License Version 1.1
 * (the "License"); you may not use this file except in compliance with the
 * License. You may obtain a copy of the License at http://www.mozilla.org/NPL/
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
 * the specific language governing rights and limitations under the License. The
 * Original Code is lutsko.com code. The Initial Developer of the Original Code
 * is James Lutsko. Portions created by the Initial Developer are Copyright (C)
 * 2003 the Initial Developer. All Rights Reserved. Contributor(s):
 * Alternatively, the contents of this file may be used under the terms of
 * either the GNU General Public License Version 2 or later (the "GPL"), or the
 * GNU Lesser General Public License Version 2.1 or later (the "LGPL"), in which
 * case the provisions of the GPL or the LGPL are applicable instead of those
 * above. If you wish to allow use of your version of this file only under the
 * terms of either the GPL or the LGPL, and not to allow others to use your
 * version of this file under the terms of the NPL, indicate your decision by
 * deleting the provisions above and replace them with the notice and other
 * provisions required by the GPL or the LGPL. If you do not delete the
 * provisions above, a recipient may use your version of this file under the
 * terms of any one of the NPL, the GPL or the LGPL. ***** END LICENSE BLOCK
 * *****
 */

package autoteam.tables;

import java.io.File;
import java.util.Vector;

import javax.swing.JOptionPane;

import autoteam.DraftCriterion;
import autoteam.League;
import autoteam.Local;

public class DraftTypeTableModel extends AutoTeamTableModel
{
    private static final long serialVersionUID = 4274279185940902832L;

    // Vector Criteria = null;
    League doc = null;
    int columns = 4;
    
    static final String[] cols = { "Criterion", "For", "Parameter", "TieBreaker" };
    
    public void hierarchical( )
    {
	columns = 1;
    }
    
    public void positional( )
    {
	columns = 4;
    }
    
    public DraftTypeTableModel( File f, League _doc )
    {
	doc = _doc;
	if ( doc.getDraftType( ) == 0 )
	    columns = 1;
	else if ( doc.getDraftType( ) == 1 )
	    columns = 4;
	
	/*
	 * if(f != null) { try { // try to read the draft if it exists File
	 * draftFile = new File(f.getAbsolutePath() + ".draft");
	 * if(!draftFile.exists()) draftFile = new
	 * File(f.getParent(),"draft.draft"); if(draftFile.exists()) {
	 * FileReader in = new FileReader(draftFile); BufferedReader d = new
	 * BufferedReader(in); String input; while((input = d.readLine()) !=
	 * null) Criteria.add(new Integer(input)); d.close(); }
	 * }catch(java.io.IOException e) {}; }
	 */
    }
    
    public int getColumnCount( )
    {
	return columns;
    } // cols.length;}
    
    public int getRowCount( )
    {
	return doc.getNumberOfCriteria( );
    }
    
    public void add( int j )
    {
	doc.addCriterion( );
    }
    
    public void delete( int r )
    {
	doc.removeCriterion( r );
    }
    
    // public String getColumnName(int col) {return (col <4 ? cols[col] :
    // null);}
    
    public String getColumnName( int col )
    {
	if ( columns == 1 )
	    return "Tie Breaker";
	else if ( columns == 4 )
	    return cols[col];
	else
	    return null;
    }
    
    public Object getValueAt( int row, int col )
    {
	Vector Criteria = doc.drafter.getCriteria( );
	if ( Criteria.size( ) <= row )
	    return null;
	DraftCriterion dc = (DraftCriterion) Criteria.get( row );
	if ( dc == null )
	    return null;
	if ( col == 0 )
	    return (dc.Skill == null ? null : doc.getSkillName( dc.Skill.intValue( ) ));
	else if ( col == 1 )
	    return dc.CriterionAsString( );
	else if ( col == 2 )
	    return dc.Parameter;
	else if ( col == 3 )
	    return (dc.Skill == null ? null : doc.getSkillName( dc.Tie.intValue( ) ));
	
	return null;
    }
    
    /*
     * JTable uses this method to determine the default renderer/ editor for
     * each cell. If we didn't implement this method, then the last column would
     * contain text ("true"/"false"), rather than a check box.
     */
    public Class getColumnClass( int c )
    {
	if ( c == 0 || c == 3 )
	    return String.class;
	return Integer.class;
    }
    
    /*
     * Enable editing.
     */

    public boolean isCellEditable( int row, int col )
    {
	return (col != 1);
    }
    
    /*
     * The action of the method depends on the drafting model we are
     * constructing. If it is hierarchical, we are just collecting an ordered
     * list of skills. If it is positional, we are collecting a list of skills
     * and criteria for how long to use them.
     */

    public void setValueAt( Object value, int row, int col )
    {
	Vector Criteria = doc.drafter.getCriteria( );
	// Make sure the array is big enough to hold the object.
	if ( Criteria.size( ) <= row )
	    Criteria.setSize( row + 1 );
	
	// Get or make the criteria object we are filling in.
	DraftCriterion dc = (DraftCriterion) Criteria.get( row );
	if ( dc == null )
	{
	    dc = new DraftCriterion( 0, 0, 0, 0 );
	    Criteria.insertElementAt( dc, row );
	}
	
	if ( col == 0 ) // translate skill name to value
	{
	    doc.setSkill( dc, value.toString( ), 0 );
	}
	else if ( col == 1 )
	{
	    if ( value != null )
		dc.setCriterion( value.toString( ) );
	    else
		dc.Criterion = null;
	    doc.setDirty( );
	}
	else if ( col == 2 )
	{
	    if ( value instanceof Integer )
	    {
		if ( value != null )
		    dc.Parameter = (Integer) value;
		else
		    dc.Parameter = null;
		doc.setDirty( );
	    }
	    else
	    {
		JOptionPane.showMessageDialog( null, Local.get( "ParamInt" ), Local.get( "Error" ),
					       JOptionPane.ERROR_MESSAGE, null );
	    }
	    
	}
	else if ( col == 3 )
	{ // translate skill name to value
	    doc.setSkill( dc, value.toString( ), 1 );
	}
	
    }
}
